﻿package tools;

import org.apache.oro.text.regex.MatchResult;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternCompiler;
import org.apache.oro.text.regex.PatternMatcher;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;

public class RegexDemo {
	public static void main(String[] args) throws Exception {
		// jdk自带的regex
		// String regex = "\\d{3}\\.\\d{3}\\.\\d{3}\\.\\d{3}";
		String regexsub = "(\\d{3})\\.(\\d{3})\\.(\\d{3})\\.(\\d{3})";
		String ip = "123.456.789.000";
		// Pattern pp = Pattern.compile(regexsub,Pattern.CASE_INSENSITIVE);
		// Matcher mm = pp.matcher("123.456.789.000");
		// 取不出来
		// MatchResult mr=mm.toMatchResult();
		// int count=mr.groupCount();
		// mr.group(1);
		// boolean b = mm.matches();
		// -----使用Jakarta-ORO库:jakarta-oro-2.0.8.jar----
		PatternCompiler pc = new Perl5Compiler();
		Pattern pattern = null;
		// 默认匹配是对大小写敏感，第二个参数表示忽略大小写
		pattern = pc.compile(regexsub, Perl5Compiler.CASE_INSENSITIVE_MASK);
		PatternMatcher pmatch = new Perl5Matcher();
		boolean b = pmatch.matches(ip, pattern);
		if (pmatch.contains(ip, pattern)) {
			// 取出来了group(0)是原数据
			MatchResult mresult = pmatch.getMatch();
			for (int i = 1; i < mresult.groups(); i++) {
				System.out.println(mresult.group(i));
			}
		}
		System.out.println(b);
	}
}
